package com.sgb.kjwl.view.widget;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.core.content.ContextCompat;

import com.sgb.kjwl.R;

/**
 * @author qiangheNoooo
 * 说明：此控件扩展性不强，只针对商品下拉筛选并且控件的外部设置宽度为layout_width = 80dp有效
 * 时间：2021.06.09
 */
public class MacTextView extends androidx.appcompat.widget.AppCompatTextView {
    private final Context context;
    private final int COUNT_TWO = 2;
    private final int COUNT_THERE = 3;
    private final int COUNT_FOUR = 4;

    public MacTextView(Context context) {
        super(context);
        this.context = context;
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public MacTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        setWidth(dip2px(context, 80));
        setHeight(dip2px(context, 30));
        setTextColor(ContextCompat.getColor(context, R.color.color_333333));
        setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
        setGravity(Gravity.CENTER);
        setSingleLine(true);
        setEllipsize(TextUtils.TruncateAt.END);
        setPadding(dip2px(context, 20), 0, dip2px(context, 20), 0);
        Drawable drawable = context.getDrawable(R.drawable.search_bg);
        setBackground(drawable);
        Drawable drawableLeft = ContextCompat.getDrawable(context, R.mipmap.ic_down_arrow_n);
        setCompoundDrawablesWithIntrinsicBounds(null, null, drawableLeft, null);
    }

    public MacTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
    }

    public void contentSelect(boolean haveContent, boolean isOpen) {
        if (!haveContent && isOpen) {
            //点击没选择 打开了弹窗
            Drawable drawableLeft = ContextCompat.getDrawable(context, R.mipmap.icon_up_red);
            setCompoundDrawablesWithIntrinsicBounds(null, null, drawableLeft, null);
            setTextColor(ContextCompat.getColor(context, R.color.color_main));
        } else if (haveContent && isOpen) {
            //点击已选择的按钮
            Drawable drawableLeft = ContextCompat.getDrawable(context, R.mipmap.icon_up_red);
            setCompoundDrawablesWithIntrinsicBounds(null, null, drawableLeft, null);
            setBackground(ContextCompat.getDrawable(context, R.drawable.search_new_blue_bg));
            setTextColor(ContextCompat.getColor(context, R.color.color_main));
        } else if (haveContent && !isOpen) {
            //点击 选择或重新选择 关闭弹窗
            Drawable drawableLeft = ContextCompat.getDrawable(context, R.mipmap.icon_down_red);
            setCompoundDrawablesWithIntrinsicBounds(null, null, drawableLeft, null);
            setBackground(ContextCompat.getDrawable(context, R.drawable.search_new_blue_bg));
        } else if (!haveContent && !isOpen) {
            //取消选择 选择不限或全国
            Drawable drawableLeft = ContextCompat.getDrawable(context, R.mipmap.ic_down_arrow_n);
            setCompoundDrawablesWithIntrinsicBounds(null, null, drawableLeft, null);
            setBackground(ContextCompat.getDrawable(context, R.drawable.search_bg));
            setTextColor(ContextCompat.getColor(context, R.color.color_tab_one));
        }
    }

    public void setMacText(CharSequence c) {
        if (c == null) {
            return;
        } else {
            setText(c);
        }
        int length = c.length();
        if (length <= COUNT_TWO) {
            setPadding(dip2px(context, 20), 0, dip2px(context, 20), 0);
        } else if (length == COUNT_THERE) {
            setPadding(dip2px(context, 15), 0, dip2px(context, 15), 0);
        } else if (length == COUNT_FOUR) {
            setPadding(dip2px(context, 10), 0, dip2px(context, 10), 0);
        } else {
            setPadding(dip2px(context, 5), 0, dip2px(context, 5), 0);
        }
    }

    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     */
    private int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
}
